//
//  HWHomeRecommendProtocol.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/24.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWHomeRecommendProtocol: NSObject {
    var model:HWHomeModel?
    
    var didClickGame:((_ :String)->())?
    /// 功能点击
    var didClickItem:((_:HWHomeGongNengModel)->())?
    /// 重磅推荐
    var didClickHeavyItem:((_:HWHomeTuiJianModel)->())?
    /// 播放
    var gameImageViewClick:((_:UIImageView,_:HWHomeGameModel)->())?
    /// 省钱卡banner点击
    var didClickSaveCardItem:((_:HWHomeMainSlideModel)->())?
    /// 结束播放
    var endPlay:(()->())?
    /// 更多分类
    var clickTagView:((_:String,_:String)->())?
    var moreButtonClick:(()->())?
    var largeCell:HWHomeGameLargeCell!
}

extension HWHomeRecommendProtocol:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if model?.fenlei?[indexPath.section - 3].style_type == "1" || model?.fenlei?[indexPath.section - 3].style_type == "4"{
            self.didClickGame?(model?.game?[indexPath.section - 3][indexPath.row].id ?? "")
        }
        
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1 {
            if (model?.tuijian?.count ?? 0) > 0 {
                return 45*widthScale
            } else {
                return 0
            }
        }else if section > 2 {
            return (45 + 18)*widthScale
        }else {
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = HWHomeTableHeader.headerOrFooterWithTableView(tableView: tableView)
        
        if section == 1 {
            header.title = "重磅推荐"
            header.detailLabel.text = ""
        } else if section == (model?.game?.count ?? 0) + 3 {
            header.title = "更多好玩"
            header.detailLabel.text = "筛选更多好玩 娱乐爽不尽"
        }else {
            header.title = model?.fenlei?[section-3].name
            header.detailLabel.text = model?.fenlei?[section-3].content
        }
        
        if section == (model?.game?.count ?? 0) + 3 {
            header.moreArrow.isHidden = false
            header.moreLabel.isHidden = false
            header.moreButton.isHidden = false
            header.moreButtonClick = {
                self.moreButtonClick?()
            }
        } else {
            header.moreArrow.isHidden = true
            header.moreLabel.isHidden = true
            header.moreButton.isHidden = true
        }
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section > 1 {
            return 10*widthScale
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footer = HWHomeTableFooter.headerOrFooterWithTableView(tableView: tableView)
        //        if section == (model?.game?.count ?? 0) + 6 - 1 {
        //            footer.contentView.backgroundColor = .white
        //        }
        return footer
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return (model?.game?.count ?? 0) + 4
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            
            return 1
        } else if section == 1 {
            if (model?.tuijian?.count ?? 0) > 0 {
                return 1
            } else {
                return 0
            }
        }else if section == 2 {
            return (model?.main_slide.count ?? 0 > 0) ? 1 : 0
        } else if section == (model?.game?.count ?? 0) + 3 {
            if (model?.fenlei?.count ?? 0) > (model?.game?.count ?? 0) {
                return 1
            } else {
                return 0
            }
        } else {
            if (model?.fenlei?[section-3].style_type == "2" || model?.fenlei?[section-3].style_type == "3") {
                return 1
            } else {
                return model?.game?[section-3].count ?? 0
            }
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ///轮播图
        let bannerCell = HWHomeBannerCell.cellWithTableView(tableView: tableView)
        ///重磅推荐
        let heavyRecommendCell = HWHomeHeavyRecommendCell.cellWithTableView(tableView: tableView)
        ///省钱卡banner
        let saveCardBanner = HWHomeSaveCardBannerCell.cellWithTableView(tableView: tableView)
        /// 大图游戏
        let largeCell = HWHomeGameLargeCell.cellWithTableView(tableView: tableView)
        /// 无大图游戏
        let normalCell = HWHomeGameNormalCell.cellWithTableView(tableView: tableView)
        /// 官方推荐
        let officialRecommendCell = HomeOfficialRecommendCell.cellWithTableView(tableView: tableView)
        /// 近期新游
        let recentCell = HWHomeRecentGameCell.cellWithTableView(tableView: tableView)
        /// 更多游戏
        let moreTypeCell = HWHomeMoreTypeCell.cellWithTableView(tableView: tableView)
        
        //        let moreCell = HWHomeMoreCell.cellWithTableView(tableView)
        if indexPath.section == 0 {
            bannerCell.model = self.model
            bannerCell.didClickBanner = {
                gameId in
                self.didClickGame?(gameId)
            }
            bannerCell.didClickItem = {
                mod in
                self.didClickItem?(mod)
            }
            return bannerCell
        }else if indexPath.section == 1 {
            heavyRecommendCell.model = self.model
            heavyRecommendCell.didClickHeavyItem = {
                mod in
                self.didClickHeavyItem?(mod)
            }
            return heavyRecommendCell
        }else if indexPath.section == 2 {
            saveCardBanner.models = model?.main_slide
            saveCardBanner.didClickItem = {
                self.didClickSaveCardItem?($0)
            }
            return saveCardBanner
        } else if indexPath.section == (model?.game?.count ?? 0) + 3 {
            moreTypeCell.model = model
            moreTypeCell.clickTagView = {
                self.clickTagView?($0,$1)
            }
            return moreTypeCell
        }else {
            /// style_type 为1或4加载大图cell或者正常类型游戏cell 2为近期新游 3为官方推荐
            if model?.fenlei?[indexPath.section - 3].style_type == "1" || model?.fenlei?[indexPath.section - 3].style_type == "4"{
                if indexPath.row == 0 {
                    largeCell.model = model?.game?[indexPath.section - 3][indexPath.row]
                    largeCell.gameImageViewClick = {
                        self.gameImageViewClick?($0,$1)
                        self.largeCell = $2
                    }
                    
                    return largeCell
                } else {
                    normalCell.model = model?.game?[indexPath.section - 3][indexPath.row]
                    return normalCell
                }
            } else if model?.fenlei?[indexPath.section - 3].style_type == "2" {
                recentCell.models = model?.game?[indexPath.section - 3]
                recentCell.didClickRecentGame = {
                    self.didClickGame?($0.id)
                }
                return recentCell
            } else if model?.fenlei?[indexPath.section - 3].style_type == "3" {
                officialRecommendCell.models = model?.game?[indexPath.section - 3]
                officialRecommendCell.didClickOfficialGame = {
                    self.didClickGame?($0.id)
                }
                return officialRecommendCell
            } else {
                return normalCell
            }
        }
    }
    
    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if self.largeCell == cell {
            self.endPlay?()
            self.largeCell = nil
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let sectionFooterHeight = 10*widthScale
        if scrollView.contentOffset.y <= sectionFooterHeight&&scrollView.contentOffset.y > 0 {
            scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: -sectionFooterHeight, right: 0)
        }else if scrollView.contentOffset.y >= sectionFooterHeight {
            scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: -sectionFooterHeight, right: 0)
        }else {
            scrollView.contentInset = .zero
        }
        
    }
    
}
